package com.wwwarehouse.taskcenter.fragment.job_point.step7;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.taskcenter.R;
import com.wwwarehouse.taskcenter.bean.job_point.JobPointRuleDetailsResponseBean;
import com.wwwarehouse.taskcenter.constant.TaskCenterConstant;
import com.wwwarehouse.taskcenter.constant.job_point.TaskCenterCommon;
import com.wwwarehouse.taskcenter.custom_view.NewLineFlexLayout;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.wwwarehouse.taskcenter.fragment.job_point.step7.ChooseJobPointRuleDetailsFragment.KEY_TEMP_INSTANCE_ID;

/**
 * Created by Jackie on 2017/11/13.
 * 设置作业点规则 选择添加方式 详情
 */

public class ChooseJobPointItemDetailsFragment extends BaseFragment {
    private View mRootView;

    private StateLayout mStateLayout;

    private TextView mNameTxt;

    private TextView mUnCoexistenceTxt;
    private TextView mCoexistenceTxt;

    private NewLineFlexLayout mStartNewLineLayout;
    private NewLineFlexLayout mEndNewLineLayout;

    private String mTempInstanceId;

    private static final int REQUEST_GET_RULE_DETAILS = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_choose_job_point_item_details, container, false);
        return mRootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
        initEvent();
    }

    private void initView() {
        mStateLayout = findView(mRootView, R.id.sl_layout);

        mNameTxt = findView(mRootView, R.id.tv_name);

        mUnCoexistenceTxt = findView(mRootView, R.id.tv_unCoexistence);
        mCoexistenceTxt = findView(mRootView, R.id.tv_coexistence);

        mStartNewLineLayout = findView(mRootView, R.id.flex_layout_start);
        mEndNewLineLayout = findView(mRootView, R.id.flex_layout_end);
    }

    private void initData() {
        TaskCenterCommon.getInstance().setStart(false);
        TaskCenterCommon.getInstance().setDetails(false);
        TaskCenterCommon.getInstance().setStartMap(null);
        TaskCenterCommon.getInstance().setEndMap(null);

        Bundle bundle = getArguments();

        if (bundle != null) {
            mTempInstanceId = bundle.getString(KEY_TEMP_INSTANCE_ID);
        }
    }

    private void initEvent() {

    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseJobPointItemDetailsFragment) {
            mActivity.setTitle(R.string.job_point_rule_details);
        }
    }

    @Override
    public void requestDatas() {
        getRuleDetails();
    }

    NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GET_RULE_DETAILS:
                    if ("0".equals(commonClass.getCode())) {
                        JobPointRuleDetailsResponseBean jobPointRuleDetailsResponseBean = JSON.parseObject(commonClass.getData().toString(), JobPointRuleDetailsResponseBean.class);

                        mNameTxt.setText(jobPointRuleDetailsResponseBean.getTempInatanceName());

                        mUnCoexistenceTxt.setVisibility(View.VISIBLE);
                        mCoexistenceTxt.setVisibility(View.VISIBLE);

                        List<JobPointRuleDetailsResponseBean.ContentBean.UnCoexistenceBean> unCoexistenceDetailsList = jobPointRuleDetailsResponseBean.getContent().getUnCoexistence();
                        List<JobPointRuleDetailsResponseBean.ContentBean.CoexistenceBean> coexistenceDetailsList  = jobPointRuleDetailsResponseBean.getContent().getCoexistence();

                        if (unCoexistenceDetailsList != null && unCoexistenceDetailsList.size() > 0) {
                            addUnCoexistenceDetails(unCoexistenceDetailsList);
                        }

                        if (coexistenceDetailsList != null && coexistenceDetailsList.size() > 0) {
                            addCoexistenceDetails(coexistenceDetailsList);
                        }
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), false);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(false);

                                getRuleDetails();
                            }
                        });
                    }
                    break;
                default:
                    break;
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(false);

                    getRuleDetails();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    /**
     * 查询作业点规则详情
     */
    private void getRuleDetails() {
        mStateLayout.showProgressView(false);

        Map map = new HashMap();

        map.put("tempInstanceId", mTempInstanceId);

        NoHttpUtils.httpPost(TaskCenterConstant.URL_GET_RULE_DETAILS, map, mOnResponseListener, REQUEST_GET_RULE_DETAILS);
    }

    /**
     * 添加货主的货不可以放在一起
     * @param unCoexistenceBeanList
     */
    private void addUnCoexistenceDetails(List<JobPointRuleDetailsResponseBean.ContentBean.UnCoexistenceBean> unCoexistenceBeanList) {
        mStartNewLineLayout.removeAllViews();

        for (JobPointRuleDetailsResponseBean.ContentBean.UnCoexistenceBean unCoexistenceBean : unCoexistenceBeanList) {
            TextView textView = new TextView(mActivity);
            textView.setLayoutParams(new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT));
            textView.setText(unCoexistenceBean.getValue() + " ");
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textView.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));

            mStartNewLineLayout.addView(textView);
        }
    }

    /**
     * 添加货主的货可以放在一起
     * @param coexistenceDetailsList
     */
    private void addCoexistenceDetails(List<JobPointRuleDetailsResponseBean.ContentBean.CoexistenceBean> coexistenceDetailsList) {
        mEndNewLineLayout.removeAllViews();

        for (JobPointRuleDetailsResponseBean.ContentBean.CoexistenceBean coexistenceBean : coexistenceDetailsList) {
            TextView textView = new TextView(mActivity);
            textView.setLayoutParams(new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.WRAP_CONTENT, ViewGroup.MarginLayoutParams.WRAP_CONTENT));
            textView.setText(coexistenceBean.getValue() + " ");
            textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
            textView.setTextColor(mActivity.getResources().getColor(R.color.common_color_c7_96999e));

            mEndNewLineLayout.addView(textView);
        }
    }
}
